﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.Domain.Query.User
{
    public class UserDetailQuery
    {
        public long? UserId { get; set; } = null;
        public long? UserIdNE { get; set; } = null;
        public string Status { get; set; } = string.Empty;
        public string IsDelete { get; set; } = string.Empty;
        public long? DeptId { get; set; } = null;
        public long? RoleId { get; set; } = null;
        public string Sex { get; set; } = string.Empty;


        /*************************可以进行模糊查询部分**********************************************/
        public string UserName { get; set; } = string.Empty;
        public string Phone { get; set; } = string.Empty;
        public string RealName { get; set; } = string.Empty;
        public string Email { get; set; } = string.Empty;
        public string NickName { get; set; } = string.Empty;

        /// <summary>
        /// 模糊查询
        /// </summary>
        public bool FuzzyQuery { get; set; } = false;

        /// <summary>
        /// 查询表达式
        /// </summary>
        /// <returns></returns>
        public Expression<Func<furion.blog.Domain.MySQL.User, bool>> Exp()
        {
            var exp = Expressionable.Create<furion.blog.Domain.MySQL.User>();
            exp.AndIF(!string.IsNullOrWhiteSpace(Status), t => t.Status == Status);
            exp.AndIF(!string.IsNullOrWhiteSpace(IsDelete), t => t.IsDelete == IsDelete);
            exp.AndIF(!string.IsNullOrWhiteSpace(Sex),t=>t.Sex == Sex);
            exp.AndIF(RoleId!=null,t=>t.RoleId == RoleId);
            exp.AndIF(DeptId != null, t => t.DeptId == DeptId);

            exp.AndIF(UserId!=null,t=>t.UserId == UserId);
            exp.AndIF(UserIdNE!=null,t=>t.UserId!= UserId);


            if (!FuzzyQuery)
            {
                exp.AndIF(!string.IsNullOrWhiteSpace(UserName), t => t.UserName == UserName);
                exp.AndIF(!string.IsNullOrWhiteSpace(RealName), t => t.RealName == RealName);
                exp.AndIF(!string.IsNullOrWhiteSpace(Phone), t => t.Phone == Phone);
                exp.AndIF(!string.IsNullOrWhiteSpace(Email), t => t.Email == Email);
                exp.AndIF(!string.IsNullOrWhiteSpace(NickName), t => t.NickName == NickName);
            }
            else
            {
                exp.AndIF(!string.IsNullOrWhiteSpace(UserName), t => t.UserName.Contains(UserName));
                exp.AndIF(!string.IsNullOrWhiteSpace(RealName), t => t.RealName.Contains(RealName));
                exp.AndIF(!string.IsNullOrWhiteSpace(Phone), t => t.Phone.Contains(Phone));
                exp.AndIF(!string.IsNullOrWhiteSpace(Email), t => t.Email.Contains(Email));
                exp.AndIF(!string.IsNullOrWhiteSpace(NickName), t => t.NickName.Contains(NickName));
            }
            return exp.ToExpression();
        }

    }
}
